package Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

import Server.AlibabaServer;
import Server.Listing;
import Server.ListingEntry;

import Client.AlibabaClient;
import Common.Utils;

import junit.framework.TestCase;

public class DedupBasicTest extends TestCase{

	AlibabaServer Server;
	
	AlibabaClient client;	// a container var.
	AlibabaClient alice;
	AlibabaClient bob;
	AlibabaClient eve;


	static final String STORAGE_LOCATION = "C:\\Users\\oshri\\workspace\\alibaba\\Stroage2";
	static final String testDir = "C:\\Users\\oshri\\workspace\\alibaba\\Test\\testFiles\\";
	static final int MAX_SIZE = 20;
	String[] clientArgs;
	List clientArgsList;

	protected void setUp() throws Exception {
		Server = new AlibabaServer();
		String[] ServerArgs = new String[1];
		ServerArgs[0] = "Stroage2";
		Server.main(ServerArgs);		

		alice = new AlibabaClient();;
		bob = new AlibabaClient();;
		eve = new AlibabaClient();;
		super.setUp();

	}
		
	// scenario 1:
	public void testDedup() throws IOException {


		byte[] hashedFileValue;
		hashedFileValue = sendFile("alice","C:\\Users\\oshri\\workspace\\alibaba\\Test\\testFiles\\dir1\\a.txt");
		assertEquals(1, Server.refList.getSize());
		sendFile("bob","C:\\Users\\oshri\\workspace\\alibaba\\Test\\testFiles\\dir1\\a.txt");
		assertEquals(1, Server.refList.getSize());

		int amount = 0;
		File StorageDir = new File(STORAGE_LOCATION);
		String[] files = StorageDir.list();
		byte[] storedFileInBytes;
		byte[] storedHashedFileValue;
		for (String fileName : files) {
			File storedFile = new File(STORAGE_LOCATION+"\\"+fileName);
			storedFileInBytes = Utils.getBytesFromFile(storedFile);
			storedHashedFileValue = Utils.getSHA256hash(storedFileInBytes);
			if (TestUtils.sameHash(storedHashedFileValue,hashedFileValue)){
				amount++;
			}

		}
		assertEquals(amount,1);
	}



	public void testDedup2() throws IOException {
	
		File toSend = new File(testDir + "dir1\\a.txt");
		byte[] fileInBytes = Utils.getBytesFromFile(toSend);
		byte[] hashedFileValue = Utils.getSHA256hash(fileInBytes);

		clientArgsList = TestUtils.setUpArgs("bf -user alice -file " + testDir+ "dir1\\a.txt", clientArgs);
		clientArgs = new String[clientArgsList.size()];
		int i=0;
		for (Object s : clientArgsList) {
			clientArgs[i] = (String)s;
			i++;
		}
		try {
			alice.main(clientArgs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		clientArgsList = TestUtils.setUpArgs("bf -user bob -file " + testDir+ "dir1\\a.txt",clientArgs);
		clientArgs = new String[clientArgsList.size()];
		i=0;
		for (Object s : clientArgsList) {
			clientArgs[i] = (String)s;
			i++;
		}
		try {
			bob.main(clientArgs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		clientArgsList = TestUtils.setUpArgs("bf -user eve -file " + testDir + "dir1\\a.txt",clientArgs);
		clientArgs = new String[clientArgsList.size()];
		i=0;
		for (Object s : clientArgsList) {
			clientArgs[i] = (String)s;
			i++;
		}
		try {
			bob.main(clientArgs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int amount = 0;
		File StorageDir = new File(STORAGE_LOCATION);
		String[] files = StorageDir.list();
		byte[] storedFileInBytes;
		byte[] storedHashedFileValue;
		for (String fileName : files) {
			File storedFile = new File(STORAGE_LOCATION+"\\"+fileName);
			storedFileInBytes = Utils.getBytesFromFile(storedFile);
			storedHashedFileValue = Utils.getSHA256hash(storedFileInBytes);
			if (TestUtils.sameHash(storedHashedFileValue,hashedFileValue)){
				amount++;
			}

		}
		assertEquals(amount,1);


	}




	private byte[] sendFile(String user, String path) throws IOException{
		File toSend = new File(path);
		byte[] fileInBytes = Utils.getBytesFromFile(toSend);
		byte[] hashedFileValue = Utils.getSHA256hash(fileInBytes);

		if (user == null){	// a default parameter
			client = alice;
		}
		else if (user.equals("alice")){
			client = alice;
		}
		else if (user.equals("bob")){
			client = bob;
		}
		else if (user.equals("eve")){
			client = eve;
		}

		clientArgsList = TestUtils.setUpArgs("bf -user "+ user+ " -file "+ path,clientArgs);
		clientArgs = new String[clientArgsList.size()];
		int i=0;
		for (Object s : clientArgsList) {
			clientArgs[i] = (String)s;
			i++;
		}
		try {
			client.main(clientArgs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hashedFileValue;
	}


}


